package projectiteu;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
//import javafx.scene.*;


public class TranslatorMain extends JFrame {
    
    JLabel label1, label2, label3;
    JButton translate, importer;
    JTextArea cpp, cs;
    JScrollPane jsp1, jsp2;
    public TranslatorMain(){
        
        super("C++ to C# Translation");
        this.setLayout(null);
        
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(dim.width/2-432, dim.height/2-350);
        
        
        label1 = new JLabel("C++ to C# Translator");
        this.add(label1);
        label1.setBounds(400,0,200,100);
        
        label2 = new JLabel("C++ (Source Code)");
        this.add(label2);
        label2.setBounds(15,50,160,80);
        
        label2 = new JLabel("C# (Target Code)");
        this.add(label2);
        label2.setBounds(450,50,160,80);
        
        cpp = new JTextArea();
        jsp1= new JScrollPane(cpp);
        this.add(jsp1);
        jsp1.setBounds(15,120,400,480);
        cpp.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        cpp.setText(
          "#include <iostream>\n"
        + "using namespace std;\n\n"
        + "int main(){\n\n\n"
        + "return 0;\n"          
        + "}"          
        );     
        cpp.setCaretPosition(cpp.getDocument().getLength()-12);
        jsp1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        
        cs = new JTextArea();
        jsp2 = new JScrollPane(cs);
        this.add(jsp2);
        jsp2.setBounds(450,120,400,480);
        cs.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        cs.setEditable(false);
        jsp2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        
        translate = new JButton("Translate");
        this.add(translate);
        translate.setBounds(380,620,100,40);
        
        importer = new JButton("Import");
        this.add(importer);
        importer.setBounds(130,80,80,20);
        
        TranslateListener tal = new TranslateListener();
        ImportListener ial = new ImportListener();
        
        importer.addActionListener(ial);
        translate.addActionListener(tal);

        

    }
    
    
    public static void main(String[] args) {
       
        TranslatorMain t = new TranslatorMain();
        t.setSize(875, 700);
        t.setVisible(true);
        t.setResizable(false);
        //added this so it would close after you click X - khalil
        t.setDefaultCloseOperation(t.EXIT_ON_CLOSE);
        
    }
    
    class TranslateListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            
            String output = new StandardLib().convertCode(cpp.getText());
            //System.out.println(cpp.getText());
            cs.setText(output);
            //System.out.print("hello");
            
            
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
        
    }
    
    class ImportListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            TranslatorMain tm = new TranslatorMain();
            JFileChooser jfc = new JFileChooser();
            jfc.showOpenDialog(tm);
            File file = jfc.getSelectedFile();
            String lines="";
            String str ="";
            try{
                FileReader files= new FileReader(file.toString());
                BufferedReader buff=  new BufferedReader(files);
         
                while((lines=buff.readLine())!=null){
                    str = str + lines + "\n";
                }
                
             
                buff.close();
            }
            catch(Exception ee){
                ee.printStackTrace();
            }
            cpp.setText(str);
            
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
        
    }
    
    
    
    
 
}